﻿cmake_minimum_required (VERSION 3.8)

project ("GLGame")
set(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR})

option(WITH_SOUND "With DirectSound8" ON)

set(C_STANDARD 99)
set(CXX_STANDARD 14)

if (CXX_STANDARD EQUAL 17)
  if (MSVC_VERSION GREATER_EQUAL "1900")
	include(CheckCXXCompilerFlag)
	CHECK_CXX_COMPILER_FLAG("/std:c++17" _cpp_17_flag_supported)
	if (_cpp_17_flag_supported)
	  set(CMAKE_CXX_STANDARD 17)
	endif()
  endif()
endif()

message(STATUS CMAKE_CXX_COMPILER_ID= "${CMAKE_CXX_COMPILER_ID}")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  if (CMAKE_BUILD_TYPE MATCHES "Debug")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEBUG")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_DEBUG")
  elseif (CMAKE_BUILD_TYPE MATCHES "Release")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O2")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O2")
  endif()
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU|Clang")
  if (CMAKE_BUILD_TYPE MATCHES "Debug")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -D_DEBUG")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -D_DEBUG")
  elseif (CMAKE_BUILD_TYPE MATCHES "Release")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O3 -Wall -Wextra")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -Wall -Wextra")
  endif()
endif()

# 将源代码添加到此项目的可执行文件。
add_library(
	mygl 
	# glad
	"external/src/glad/glad.c"
	# util
	"src/mygl/utils/util.cpp"
	# common
	"src/mygl/common/shader.cpp"
	"src/mygl/common/es_image.cpp" 
	"src/mygl/common/texture2d.cpp" 
	"src/mygl/common/resource_manager.cpp" 
	"src/mygl/common/sprite_renderer.cpp" 
	"src/mygl/common/text_renderer.cpp"
	# game
	"src/mygl/game/game.cpp" 
	"src/mygl/game/game_object.cpp" 
	"src/mygl/game/game_level.cpp" 
	"src/mygl/game/ball_object.cpp" 
	"src/mygl/game/particle_generator.cpp" 
	"src/mygl/game/post_processing.cpp" 
	"src/mygl/game/power_up.cpp" 
) 
target_include_directories(mygl PUBLIC "external/include")
target_include_directories(mygl PUBLIC "external/include/freetype2")
target_include_directories(mygl PUBLIC "include")
target_link_directories(mygl PUBLIC lib)
target_link_libraries(mygl PUBLIC glfw3 freetype)

if (CMAKE_BUILD_TYPE MATCHES "Debug")
  target_compile_definitions(mygl PUBLIC "-DPROJECT_ROOT=\"${PROJECT_ROOT}\"")
endif()

if (WITH_SOUND)
  target_link_libraries(mygl PUBLIC irrKlang ${PROJECT_ROOT}/lib/irrKlang.exp)
  target_compile_definitions(mygl PUBLIC "-DUSE_SOUND")
endif()

# Breakout
add_executable (BreakOut "break_out.cpp")
target_link_libraries(BreakOut mygl)

# TODO: install
install(TARGETS mygl)
install(TARGETS BreakOut)
install(DIRECTORY ${PROJECT_ROOT}/bin/ DESTINATION bin)
install(DIRECTORY ${PROJECT_ROOT}/lib/ DESTINATION lib)
install(DIRECTORY ${PROJECT_ROOT}/resources/ DESTINATION resources)
install(DIRECTORY ${PROJECT_ROOT}/shaders/ DESTINATION shaders)